package com.xintoucloud.pbs.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.pbs.dao.MenuDao;
import com.xintoucloud.pbs.entity.Menu;
import com.xintoucloud.pbs.entity.Organization;
import com.xintoucloud.pbs.entity.PartyMemberPermission;
import com.xintoucloud.pbs.entity.PartyMemberPermissionMenu;
import com.xintoucloud.pbs.model.Attributes;
import com.xintoucloud.pbs.model.CheckArr;
import com.xintoucloud.pbs.model.Node;
import com.xintoucloud.pbs.model.Status;
import com.xintoucloud.pbs.model.TreeData;
import com.xintoucloud.pbs.model.TreeNode;
import com.xintoucloud.pbs.service.MenuService;
import com.xintoucloud.pbs.service.PartyMemberPermissionMenuService;
import com.xintoucloud.pbs.service.PartyMemberPermissionService;
@Service@Transactional
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private PartyMemberPermissionMenuService partyMemberPermissionMenuService;
	@Override
	public List<Node> listNodeByParentId(Integer parentId,Integer ppid) {
		//
		List<PartyMemberPermissionMenu> pmpmms=partyMemberPermissionMenuService.listByPartyMemberPermissionppid(ppid);
		List<Integer> pmids=new ArrayList<>();
		for (PartyMemberPermissionMenu partyMemberPermissionMenu : pmpmms) {
			if(partyMemberPermissionMenu.getState()!=2) {
			pmids.add(partyMemberPermissionMenu.getMenu_pmid());
			}
		}
		
		List<Menu> menus=null;
				if(parentId==0) {
					//查找所有父节点为0的
					menus=menuDao.listMenuOnNullParentId();
				}else {
					menus=menuDao.listMenusByParentId(parentId);
				}
				List<Node> nodes=new ArrayList<>();
				for (Menu menu : menus) {
					Node node=new Node();
					node.setIconCls(menu.getIconCls());
					node.setId(menu.getId());
					node.setText(menu.getMenuName());
					Attributes attributes=new Attributes();
					attributes.setUrl(menu.getUrl());
					node.setAttributes(attributes);
					List<Node> childs=listNodeByParentId(menu.getId(),ppid);
					node.setChildren(childs);
					if(childs!=null) {
						if(pmids.contains(node.getId())) {
							nodes.add(node);
						}
					node.setState("closed");
					}else {
					node.setState("open");
					}
				}
				return nodes;
		}
	
	@Override
	public TreeData listTreeDataByParentId(Integer parentId, Integer ppid) {
		TreeData treeData=new TreeData();
		List<TreeNode> data=listTreeNode(parentId, ppid);
		treeData.setData(data);
		Status status=new Status();
		status.setCode(200);
		status.setMessage("加载成功");
		treeData.setStatus(status);
		return treeData;
	}
	public List<TreeNode> listTreeNode(Integer parentId, Integer ppid) {
		List<PartyMemberPermissionMenu> pmpmms=partyMemberPermissionMenuService.listByPartyMemberPermissionppid(ppid);
		List<Integer> pmids=new ArrayList<>();
		for (PartyMemberPermissionMenu partyMemberPermissionMenu : pmpmms) {
			pmids.add(partyMemberPermissionMenu.getMenu_pmid());
		}
		List<Menu> menus=null;
		if(parentId==0) {
			//查找所有父节点为0的
			menus=menuDao.listMenuOnNullParentId();
		}else {
			menus=menuDao.listMenusByParentId(parentId);
		}
		List<TreeNode> treeNodes=new ArrayList<>();
		int level=1;
		for (Menu menu : menus) {
			TreeNode treeNode=new TreeNode();
			treeNode.setId(menu.getId());
			treeNode.setTitle(menu.getMenuName());
			if(menu.getParentId()==0) {
			treeNode.setLevel(level);
			}
			treeNode.setParentId(menu.getParentId());
			List<CheckArr> checkArrs=new ArrayList<>();
			CheckArr checkArr=new CheckArr();
			checkArr.setIsChecked("0");
			checkArr.setType("0");
			checkArrs.add(checkArr);
			treeNode.setCheckArr(checkArrs);
			List<TreeNode> childs=listTreeNode(menu.getId(),ppid);
			treeNode.setChildren(childs);
			if(childs.isEmpty()) {
				treeNode.setisLast(true);
			}else {
				treeNode.setisLast(false);
				treeNode.setLevel(level++);
			}
			if(pmids.contains(treeNode.getId())) {
				treeNodes.add(treeNode);
			}
			
		}
		return treeNodes;
	}
}
